const db = require('../db/index')
const bcrypt = require('bcryptjs')
const moment = require('moment')

exports.getdayPlants = (req, res) => {
	console.log(req.user)
	let plantTime = moment().format('YYYY-MM-DD')
	console.log(plantTime)
	const sql =
		'select plantId,plantTitle,plantTime,plantStart,plantEnd,plantNotes,is_start,is_end from plant where userId=? and plantTime=?'
	console.log(req.user.user_id)
	db.query(sql, [req.user.user_id, plantTime], (err, results) => {
		if (err) return res.cc(err)
		console.log('111')
		console.log(results)
		if (results.length == 0) return res.cc('获取计划信息失败！')
		res.send({
			status: 0,
			message: '获取计划信息成功！',
			data: results
		})
	})
}

exports.postaddPlant = (req, res) => {
	console.log(req.user)
	const plant = req.body
	console.log('添加计划')
	console.log(plant)
	const insertSql = 'insert into plant set ?'
	db.query(insertSql, {
		userId: req.user.user_id,
		plantTitle: plant.plantTitle,
		plantTime: plant.plantTime,
		plantStart: plant.plantStart,
		plantEnd: plant.plantEnd,
		plantNotes: plant.plantNotes,
		emon: plant.emon,
		is_start: plant.is_start,
		is_end: plant.is_end
	}, function(err, results) {
		if (err) {
			return res.cc(err)
		}
		if (results.affectedRows != 1) {
			return res.cc('添加失败，请稍后重试！')
		}
		res.cc('添加成功！', 0)
	})

}
// 修改提醒
exports.postupPlantTime = (req, res) => {
	console.log(req.user)
	const remind = req.body
	console.log('提醒')
	console.log(remind)
	// let is_can = remind.oneTwo?is_start:is_end
	let is_bian = remind.bian == 'true' ? 'true' : 'false'
	const updateSql = remind.oneTwo == 'true' ? `update plant set is_start=? where plantId=?` :
		`update plant set is_end=? where plantId=?`
	console.log(updateSql)
	db.query(updateSql, [is_bian, remind.plantId], (err, results) => {
		if (err) return console.log(err.message)
		// 行数
		// 插入则results是一个对象
		if (results.affectedRows === 1) {
			console.log('更新数据成功')
			res.cc('更新成功！', 0)
		}
	})
}
